#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "./lcd/bsp_ili9341_lcd.h"
#include "usart.h"
#include "key.h"
#include "stdio.h"
#include "limits.h"

#define USE_CHAR 0
static TaskHandle_t AppTaskCreate_Handle = NULL;
static TaskHandle_t receive1_task_handle = NULL;
static TaskHandle_t receive2_task_handle = NULL;
static TaskHandle_t send_task_handle = NULL;


static void AppTaskCreate(void);
static void Receive1_Task(void* pvParameters);
static void Receive2_Task(void* pvParameters);
static void Send_Task(void* pvParameters);

int main(void)
{ 
	BaseType_t xReturn = pdPASS;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	KEY_Init();
	LED_Init();		        //初始化LED端口
	ILI9341_Init();//液晶初始化    
	LCD_SetColors(BLACK,WHITE);//设置白底黑字
	LCD_SetFont(&Font8x16);//设置字体大小
	ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);//清屏
  ILI9341_GramScan ( 6 );//设置显示模式
	USART_Config(115200);
  LED_Color(LED_OFF);
	
	xReturn = xTaskCreate((TaskFunction_t )AppTaskCreate,  
                        (const char*    )"AppTaskCreate",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )1, 
                        (TaskHandle_t*  )&AppTaskCreate_Handle);
	if(pdPASS == xReturn){
		vTaskStartScheduler();          //开启任务调度
	}
  else{
		printf("Error Start RTOS\n");
	}
	
}


static void AppTaskCreate(void)
{
  BaseType_t xReturn = pdPASS;
  
  taskENTER_CRITICAL();   
    
	xReturn = xTaskCreate((TaskFunction_t )Receive1_Task,  
                        (const char*    )"Receive1_Task",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )2, 
                        (TaskHandle_t*  )&receive1_task_handle);
  if(xReturn != NULL)
	{
		printf("Success Create Receive1_Task\r\n");
	}
  
	xReturn = xTaskCreate((TaskFunction_t )Receive2_Task,  
                        (const char*    )"Receive2_Task",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )3, 
                        (TaskHandle_t*  )&receive2_task_handle);
  if(xReturn != NULL)
	{
		printf("Success Create Receive2_Task\r\n");
	}
	
	xReturn = xTaskCreate((TaskFunction_t )Send_Task,  
                        (const char*    )"Send_Task",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )4, 
                        (TaskHandle_t*  )&send_task_handle);
  if(xReturn != NULL)
	{
		printf("Success Create Send_Task\r\n");
	}
	
  vTaskDelete(AppTaskCreate_Handle);
  taskEXIT_CRITICAL();          
}


static void Receive1_Task(void* pvParameters)
{
	BaseType_t xReturn = pdTRUE;
#if USE_CHAR
	char * r_char;
#else
	uint32_t r_num;
#endif
	while(1)
	{
		xReturn = xTaskNotifyWait(0x0,
															ULONG_MAX,
#if USE_CHAR
															(uint32_t*)r_char,
#else
															&r_num,
#endif
															portMAX_DELAY
		);
		if(pdTRUE == xReturn)
		{
#if USE_CHAR
			printf("Receive1_Task:%s\n",r_char);
#else
			printf("Receive1_Task:%d\n",r_num);
#endif
		}
	
	}
}
static void Receive2_Task(void* parameter)
{	
  BaseType_t xReturn = pdTRUE;
#if USE_CHAR
  char *r_char;
#else
  uint32_t r_num;
#endif
  while (1)
  {
		xReturn=xTaskNotifyWait(0x0,			
                            ULONG_MAX,	 
#if USE_CHAR
                            (uint32_t *)&r_char,		  
#else
                            &r_num,		 
#endif                        
                            portMAX_DELAY);
    if( pdTRUE == xReturn )
		{
#if USE_CHAR
			printf("Receive2_Task:%s\n",r_char);
#else
			printf("Receive2_Task:%d\n",r_num);
#endif
		}
  }
}
static void Send_Task(void* parameter)
{	 
  BaseType_t xReturn = pdPASS;
#if USE_CHAR
  char test_str1[] = "this is a mail test 1";
  char test_str2[] = "this is a mail test 2";
#else
  uint32_t send1 = 1;
  uint32_t send2 = 2;
#endif

  while (1)
  {
    if(GET_KEY1)
    {
      xReturn = xTaskNotify( receive1_task_handle,
#if USE_CHAR 
                             (uint32_t)&test_str1, 
#else
                              send1,
#endif
                             eSetValueWithOverwrite );
      
      if( xReturn == pdPASS )
        printf("Send Success Receive1_Task_Handle\r\n");
    } 
    if(GET_KEY2)
    {
      xReturn = xTaskNotify( receive1_task_handle, 
#if USE_CHAR 
                             (uint32_t)&test_str2, 
#else
                              send2,
#endif
                             eSetValueWithOverwrite );
 
      if( xReturn == pdPASS )
        printf("Send Success Receive2_Task_Handle\r\n");
    }
    vTaskDelay(20);
  }
}